package br.com.cnpv.modelo.bo;

import java.util.List;

import org.hibernate.Session;

import br.com.cnpv.modelo.acesso.idao.IAssociacaoDAO;
import br.com.cnpv.modelo.acesso.idao.IVeiculoDAO;
import br.com.cnpv.modelo.dao.AssociacaoDAO;
import br.com.cnpv.modelo.dao.VeiculoDAO;
import br.com.cnpv.modelo.entity.Associacao;
import br.com.cnpv.modelo.entity.Veiculo;
import br.com.cnpv.util.HibernateUtil;

public class VeiculoBO extends HibernateUtil
{
	public static VeiculoBO INSTANCE;
	
	public static VeiculoBO getInstance()
	{
		if(INSTANCE == null)
		{
			synchronized (VeiculoBO.class) {
				INSTANCE = new VeiculoBO();
			}
		}
		return INSTANCE;
	}

	public List<Veiculo> buscar(Veiculo veiculo) throws Exception {
		Session session = null;
		List<Veiculo> list = null;
		try
		{
			session = currentSession();
			IVeiculoDAO dao = new VeiculoDAO();
			list = dao.buscar(veiculo, session);
		}catch(Exception e)
		{
			throw new Exception(e);
		}finally
		{
			if(session != null)
			{
				session.flush();
				session.clear();
				closeSession();
			}
		}
		return list;
	}
	
	public List<Veiculo> listar() throws Exception
	{
		Session session = null;
		List<Veiculo> list = null;
		try
		{
			session = currentSession();
			IVeiculoDAO dao = new VeiculoDAO();
			list = dao.listar(session);
		}catch(Exception e)
		{
			throw new Exception(e);
		}finally
		{
			if(session != null)
			{
				session.flush();
				session.clear();
				closeSession();
			}
		}
		return list;
	}
}